const mongoose = require('mongoose');

/**
 * 糖尿病风险评估模型
 */
const RiskAssessmentSchema = new mongoose.Schema(
  {
    user: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User',
      required: true
    },
    result: {
      prediction: {
        type: Number, // 0 或 1，表示阴性或阳性
        required: true
      },
      probability: {
        type: Number, // 概率值，0-1之间
        required: true
      },
      riskLevel: {
        type: String,
        enum: ['低风险', '中风险', '高风险'],
        required: true
      }
    },
    inputData: {
      Age: {
        type: Number,
        required: true
      },
      Glucose: {
        type: Number,
        required: true
      },
      BloodPressure: {
        type: Number,
        required: true
      },
      BMI: {
        type: Number,
        required: true
      },
      DiabetesPedigreeFunction: {
        type: Number,
        required: true
      },
      Pregnancies: {
        type: Number,
        default: 0
      },
      SkinThickness: {
        type: Number
      },
      Insulin: {
        type: Number
      }
    },
    measuredAt: {
      type: Date,
      default: Date.now
    },
    notes: {
      type: String
    }
  },
  {
    timestamps: true
  }
);

// 创建评估记录索引以加快查询速度
RiskAssessmentSchema.index({ user: 1, measuredAt: -1 });

module.exports = mongoose.model('RiskAssessment', RiskAssessmentSchema); 